package edu.lingnan.blog.auth.service;


import edu.lingnan.blog.api.RemoteUserService;
import edu.lingnan.blog.api.model.LoginUser;
import edu.lingnan.blog.common.core.constant.SecurityConstants;
import edu.lingnan.blog.common.core.constant.UserConstants;
import edu.lingnan.blog.common.core.domain.R;
import edu.lingnan.blog.common.core.domain.pojo.User;
import edu.lingnan.blog.common.core.enums.UserStatus;
import edu.lingnan.blog.common.core.exception.ServiceException;
import edu.lingnan.blog.common.core.utils.StringUtils;
import edu.lingnan.blog.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 登录校验方法
 *
 * @author cql
 */
@Service
public class LoginService {


    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 登录(验证码在网关处验证)
     *
     * @param account
     * @param password
     * @return
     */
    public LoginUser login(String account, String password) {
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(account, password)) {
            throw new ServiceException("用户/密码必须填写");
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            throw new ServiceException("用户密码不在指定范围");
        }
        // 用户名不在指定范围内 错误
        if (account.length() != UserConstants.ACCOUNT_LENGTH) {
            throw new ServiceException("用户名不在指定范围");
        }

        // 查询用户信息
        R<LoginUser> userResult = remoteUserService.getUserInfo(account, SecurityConstants.INNER);

        if (R.FAIL == userResult.getCode()) {
            throw new ServiceException(userResult.getMsg());
        }

        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
            throw new ServiceException("登录用户：" + account + " 不存在");
        }

        LoginUser userInfo = userResult.getData();
        User user = userResult.getData().getUser();
        if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            throw new ServiceException("对不起，您的账号：" + account + " 已被删除");
        }
        if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
            throw new ServiceException("用户不存在/密码错误");
        }
        return userInfo;
    }


    /**
     * 用户注册
     *
     * @param account
     * @param password
     */
    public void register(String account, String password) {
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(account, password)) {
            throw new ServiceException("用户/密码必须填写");
        }
        if (account.length() != UserConstants.ACCOUNT_LENGTH) {
            throw new ServiceException("账户长度必须为11个字符");
        }
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            throw new ServiceException("密码长度必须在5到20个字符之间");
        }
        User user = new User();
        user.setAccount(account);
        user.setPassword(SecurityUtils.encryptPassword(password));
        R<?> registerResult = remoteUserService.registerUserInfo(user, SecurityConstants.INNER);
        if (R.FAIL == registerResult.getCode()) {
            throw new ServiceException(registerResult.getMsg());
        }
    }
}
